package org.edu.modules.common.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.edu.modules.common.entity.VeBaseDictionary;
import org.edu.modules.common.mapper.VeBaseDictionaryMapper;
import org.edu.modules.common.service.IVeBaseDictionaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther 李少君
 * @Date 2021-12-13 16:24
 */
@Service
public class VeBaseDictionaryServiceImpl extends ServiceImpl<VeBaseDictionaryMapper, VeBaseDictionary> implements IVeBaseDictionaryService {

    @Autowired
    private VeBaseDictionaryMapper veBaseDictionaryMapper;

    @Override
    public List<VeBaseDictionary> getDictionaryListByCode(String code) {
        return veBaseDictionaryMapper.getDictionaryListByCode(code);
    }

    @Override
    public List<Map<String, Object>> getDictDataListByCode(String code) {
        return veBaseDictionaryMapper.getDictDataListByCode(code);
    }

    @Override
    public List<VeBaseDictionary> getTreeList() {
        List<VeBaseDictionary> rootList = this.veBaseDictionaryMapper.getRootList();
        List<VeBaseDictionary> bodyList = this.veBaseDictionaryMapper.getBodyList();
        if (bodyList != null && !bodyList.isEmpty()) {
            Map<Integer, Integer> map = new HashMap<>();
            rootList.forEach(menu -> getChild(menu, map));
            return rootList;
        }
        return null;
    }

    public void getChild(VeBaseDictionary menu, Map<Integer, Integer> map) {
        List<VeBaseDictionary> bodyList = this.veBaseDictionaryMapper.getBodyList();
        List<VeBaseDictionary> childList = new ArrayList<>();
        bodyList.stream().filter(c -> !map.containsKey(Integer.valueOf(c.getId()))).filter(c -> (c.getPid() == menu.getId()))
                .forEach(c -> {
                    map.put(Integer.valueOf(c.getId()), Integer.valueOf(c.getPid()));
                    getChild(c, map);
                    childList.add(c);
                });
        menu.setVeBaseDictionaryList(childList);
    }
}
